<?php
    class AdminDthink_Model_User extends Zend_Db_Table_Abstract
    {
        protected $_name = 'user';
        protected $_primary = 'user_id';
        protected $_db;
        
        public function __construct() {
            $this->_db = Zend_Registry::get('db');
        }
        
        public function listUser() {
            $data = array(
                'user_id',
                'email',
                'fullname',
                'avatar',
                'level',
                'status',
            );
            $sql = $this->_db->select()
                             ->from($this->_name)
                             ->order('user_id DESC');
            return $sql;
        }
        
        public function checkUser($email, $user_id = '') {
            if($user_id != '') {
                $sql = $this->_db->select()
                                ->from($this->_name)
                                ->where('email = ?', $email)
                                ->where('user_id <> ?', $user_id);
                $result = $this->_db->fetchAll($sql);
                return count($result);
            }
            else {
                $sql = $this->_db->select()
                                ->from($this->_name)
                                ->where('email = ?', $email);
                $result = $this->_db->fetchAll($sql);
                return count($result);
            }
        }
        
        public function getUserById($user_id) {
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->where('user_id = ?', $user_id);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function createUser($data) {
            $this->_db->insert($this->_name, $data);
        }
        
        public function editUser($data, $user_id) {
            $where = $this->getAdapter()->quoteInto('user_id = ?', $user_id);
            $result = $this->_db->update($this->_name, $data, $where);
            if($result) {
            	return true;
            }
            else {
            	return false;
            }
        }
        
        public function deleteUser($user_id) {
            $where = $this->getAdapter()->quoteInto('user_id = ?', $user_id);
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->where($where);
            $row = $this->_db->fetchRow($sql);
            if (is_file(PUBLIC_PATH . '/files/user/' . $row['avatar'])) {
                unlink(PUBLIC_PATH . '/files/user/' . $row['avatar']);
            }
            $this->_db->delete($this->_name, $where);
        }
        
        public function deleteUsers($listId) {
            foreach($listId as $value) {
                $where = $this->getAdapter()->quoteInto('user_id = ?', $value);
                $sql = $this->_db->select()
                                 ->from($this->_name)
                                 ->where($where);
                $row = $this->_db->fetchRow($sql);
                if (is_file(PUBLIC_PATH . '/files/user/' . $row['avatar'])) {
                    unlink(PUBLIC_PATH . '/files/user/' . $row['avatar']);
                }
                $this->_db->delete($this->_name, $where);
            }
        }
        
        public function publishUser($data, $user_id) {
            $user_id = (int) $user_id;
            $where = $this->getAdapter()->quoteInto('user_id = ?', $user_id);
            $this->_db->update($this->_name, $data, $where);
        }
        
        public function searchUser($keyword) {
            $keyword = (string) '%' . str_replace(' ', '%', $keyword) . '%';
            $data = array(
                'user_id',
                'email',
                'fullname',
                'avatar',
                'level',
                'status',
            );
            $sql = $this->_db->select()
                            ->from($this->_name, $data)
                            ->where('fullname_plaintext LIKE ?', $keyword)
                            ->orWhere('email LIKE ?', $keyword)
                            ->order('fullname ASC');
            return $sql;
        }
        
        public function delImg($user_id) {
            $data = array(
                'avatar' => 0,
            );
            $where = $this->getAdapter()->quoteInto('user_id = ?', $user_id);
            $this->_db->update($this->_name, $data, $where);
        }
        
        public function checkPassword($user_id, $password) {
            $where = $this->_primary . " = " . $user_id . " AND password = '" . md5($password) . "'";
            $sql = $this->_db->select()
                             ->from($this->_name)
                             ->where($where);
            $result = $this->_db->fetchAll($sql);
            if(count($result) > 0) {
                return true;
            }
            else {
                return false;
            }
        }
        
        public function changePasswordUser($user_id, $newPassword) {
            $data = array(
                'password' => md5($newPassword),
            );
            $where = $this->getAdapter()->quoteInto('user_id = ?', $user_id);
            $result = $this->_db->update($this->_name, $data, $where);
            if($result) {
                return true;
            }
            else {
                return false;
            }
        }
    }
